package com.nep.NEPM.controller;


import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class AddAQIDataController {
    @FXML
    private TextField txtLevel;
    @FXML private TextField txtDescription;
    @FXML private TextField txtCount;

    private Stage dialog;
    private AQIController.AQIData newData;  // 添加这行声明

    // 初始化方法（可选）
    @FXML
    private void initialize() {
        // 可以添加初始化逻辑
    }

    // 设置对话框引用
    public void setDialog(Stage dialog) {
        this.dialog = dialog;
    }

    @FXML
    private void handleConfirm() {
        try {
            String level = txtLevel.getText().trim();
            String description = txtDescription.getText().trim();
            String count = txtCount.getText().trim();

            // 验证输入
            if (level.isEmpty() || description.isEmpty() || count.isEmpty()) {
                showAlert("错误", "所有字段都必须填写");
                return;
            }

            if (!level.matches("\\d+")) {
                showAlert("错误", "等级必须是数字");
                return;
            }

            if (!count.matches("\\d+")) {
                showAlert("错误", "分布统计必须是数字");
                return;
            }

            // 使用正确的构造函数
            newData = new AQIController.AQIData(
                    Integer.parseInt(level),
                    description,
                    Integer.parseInt(count)
            );

            dialog.close();
        } catch (NumberFormatException e) {
            showAlert("错误", "数字格式不正确");
        }
    }

    @FXML
    private void handleCancel() {
        newData = null;
        dialog.close();
    }

    // 获取新增的数据（供外部调用）
    public AQIController.AQIData getNewData() {
        return newData;
    }

    private void showAlert(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}